package com.zhy.http.okhttp.utils;

import android.text.TextUtils;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by BLACK on 2017/12/21.
 */

public class StringUtil {
    public static DecimalFormat decima1Format = new DecimalFormat("0.0");

    public static String tostLongString(String sum) {
        decima1Format.setRoundingMode(RoundingMode.FLOOR);
        if (sum != null && isNumeric(sum)) {
            String s = "";
            float aLong = Float.valueOf(sum);
            if (aLong > 10000) {
                aLong = aLong / 10000f;
                s += "万";
                sum = decima1Format.format(aLong);
            }
            return sum + s;
        }
        return "0";
    }

    /**
     * 检查不为空
     *
     * @param str
     * @return true表示有参数 false没有参数
     */
    public static boolean checkStringNoNull(String str) {
        if (str == null || str.equals("")) {
            return false;
        }
        return true;
    }

    //是否數字 包括负数
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("^[0-9]+(.[0-9]+)?$");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches() || str.trim().equals("")) {
            return false;
        }
        return true;
    }

    /**
     * 判断手机号是否有效
     *
     * @param phoneNum 手机号
     * @return 有效则返回true, 无效则返回false
     */
    public static boolean isPhoneNumValid(String phoneNum) {
        return phoneNum.length() == 11 && phoneNum.matches("^((1[3,7,8][0-9])|(14[5,7])|(15[^4\\D])|(166)|(17[3,5,6,8])|(19[8,9]))\\d{8}$");
    }


    /**
     * 保留两位小数
     *
     * @param f
     * @return
     */
    public static String fromTowDecimal(float f) {
        DecimalFormat df = new DecimalFormat("#0.00");
        return df.format(f);
    }

    public static String fromTowDecimal(String f) {
        return fromTowDecimal(Float.valueOf(f));
    }

}
